package com.jams.goodox.activity.serve.repair;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.jams.goodox.activity.serve.BaseServeActivity;
import com.jams.goodox.activity.serve.ServeSureOrderActivity;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.RepairStandBean;
import com.jams.goodox.entity.ServeSubmitBean;
import com.jams.goodox.entity.ServicePeriod;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.DateTimePickerDialog;

import java.util.List;

import retrofit2.Call;

/**
 * 重构报修页面
 * Created by guozhen.hou on 2017/7/7.
 */

public class AddRepairActivity extends BaseServeActivity {

    private String id;//服务项id
    private int type;//服务类型
    private RepairStandBean.DataBean stringBean;

    private List<String> dateList;
    private List<String> timeZeroList;
    private List<String> timeNormalList;

    private DateTimePickerDialog dateTimePickerDialog;//服务时间选择器
    private String serviceDay;//选择的日期
    private String serviceTime;//选择的时间

    /**
     * 接收上个页面传递的数值
     *
     * @param context  页面
     * @param type     企业维修or公共维修
     * @param barTitle 选中的服务名称，页面标题
     * @param id       用于获取收费标准的id
     * @param subId    服务id
     * @param title    大标题，一般是维修
     */
    public static void go(Context context, int type, String barTitle, String id, String subId, String title,String busId) {
        Intent in = new Intent(context, AddRepairActivity.class);
        in.putExtra("type", type);
        in.putExtra("barTitle", barTitle);
        in.putExtra("id", id);
        in.putExtra("subId", subId);
        in.putExtra("title", title);
        in.putExtra("busId",busId);
        context.startActivity(in);
    }

    @Override
    protected String getBarTitle() {
        return getIntent().getStringExtra("barTitle");
    }

    @Override
    protected void submitServeOrder() {
        if (!hasDescription()) {
            ToastUtil.show(this, "请输入问题描述");
            return;
        }
        if (type == Constant.SERVE_TYPE_PUBLIC) {
            goNext();
        } else {
            getServiceDateTime();
        }
    }

    /**
     * 获取服务时间
     */
    private void getServiceDateTime() {
        enqueue(createCallService(ServiceService.class).getServicePeriod(subId), new RetrofitCallbackListener<ServicePeriod>() {
            @Override
            public void onSuccess(Call<ServicePeriod> call, ServicePeriod result) {
                if (result.getData() == null) return;
                dateList = result.getData().getDays();
                timeZeroList = result.getData().getTimeZero();
                timeNormalList = result.getData().getTimeNormal();
                if (result.getData().getIsOpenTime() != 0) {
                    goNext();
                } else {
                    showNumPicker();
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.show(msg);
            }
        });
    }

    /**
     * 弹出服务时间框
     */
    private void showNumPicker() {
        if (dateTimePickerDialog == null) {
            dateTimePickerDialog = new DateTimePickerDialog(this, dateList, timeZeroList, timeNormalList);
            dateTimePickerDialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
                @Override
                public void OnDateTimeSet(AlertDialog dialog, String date, String hours) {
                    serviceDay = date;
                    serviceTime = hours;
                    goNext();
                    dialog.cancel();
                }
            });
        }
        dateTimePickerDialog.show();
    }

    /**
     * 与此统一提交维修信息
     */
    private void goNext() {
        ServeSubmitBean serveSubmitBean = getSubmitWithBaseParams();
        String subTitle = String.format("%s-%s", (type == Constant.SERVE_TYPE_PUBLIC) ? "企业维修" : "居家维修", getBarTitle());//子标题需要区分企业维修or公共维修
        serveSubmitBean.setType(type);
        serveSubmitBean.setItemName(subTitle);
        serveSubmitBean.setServiceDay(serviceDay);
        serveSubmitBean.setServiceTime(serviceTime);
        serveSubmitBean.setRepairId(id);
        serveSubmitBean.setTitle(getIntent().getStringExtra("title"));
        serveSubmitBean.setBusId(getIntent().getStringExtra("busId"));
        ServeSureOrderActivity.go(this, serveSubmitBean);
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        type = getIntent().getIntExtra("type", 0);
        if (type == Constant.SERVE_TYPE_PUBLIC) {
            StyleUtil.titleBackKey(this, getBarTitle());
        } else
            StyleUtil.titleBackKeyWithRightTextViewListener(this, getBarTitle(), "收费标准", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (stringBean == null) {
                        getRepairStand();
                        return;
                    }
                    goActivity();
                }
            });
        setEditHint("请输入问题描述，以便于我们准确定位，尽快处理");
    }

    /**
     * 获取维修收费标准
     */
    private void getRepairStand() {
        showLoading();
        enqueue(createCallService(ServiceService.class).getRepairStand(id), new RetrofitCallbackListener<RepairStandBean>() {
            @Override
            public void onSuccess(Call<RepairStandBean> call, RepairStandBean result) {
                misLoading();
                stringBean = result.getData();
                goActivity();
            }

            @Override
            public void onError(String msg) {
                ToastUtil.show(msg);
                misLoading();
            }
        });
    }

    /**
     * 跳转置收费标准页面
     */
    private void goActivity() {
        if (stringBean == null) return;
        Intent in = new Intent(AddRepairActivity.this, RepairWebView.class);
        in.putExtra("content", stringBean.getFeeScale());
        in.putExtra("barTitle", "收费标准");
        startActivity(in);
    }
}
